package com.mango.game.controller.web;

import com.mango.game.dtos.ListDTO;
import com.mango.game.dtos.NameSpaceDTO;
import com.mango.game.dtos.QueryDTO;
import com.mango.game.dtos.ResponseDTO;
import com.mango.game.entity.NameSpace;
import com.mango.game.service.INameSpaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 命名空间 前端控制器
 * </p>
 *
 * @author Mango
 * @since 2025-02-24
 */
@RestController
public class NameSpaceController {

    @Autowired
    private INameSpaceService nameSpaceService;

    @PostMapping("/namespace-query")
    public ResponseEntity<ResponseDTO> query(@RequestBody QueryDTO<NameSpace> dto) {
        return ResponseEntity.ok(nameSpaceService.customQuery(dto));
    }

    @PostMapping("/namespace-list")
    public ResponseEntity<ResponseDTO> list(@RequestBody ListDTO<NameSpace> dto) {
        return ResponseEntity.ok(nameSpaceService.customList(dto));
    }

    @PostMapping("/namespace-save")
    public ResponseEntity<ResponseDTO> save(@RequestBody NameSpaceDTO dto) {
        if (StringUtils.isEmpty(dto.getName())) {
            return ResponseDTO.failure("命名空间名称不能为空");
        }
        ResponseDTO resp = nameSpaceService.customSave(dto);
        return ResponseEntity.ok(resp);
    }

    @PostMapping("/namespace-update")
    public ResponseEntity<ResponseDTO> update(@RequestBody NameSpaceDTO dto) {
        if (StringUtils.isEmpty(dto.getName())) {
            return ResponseDTO.failure("命名空间名称不能为空");
        }
        if (StringUtils.isEmpty(dto.getUid())) {
            return ResponseDTO.failure("命名空间ID不能为空");
        }
        ResponseDTO resp = nameSpaceService.customUpdate(dto);
        return ResponseEntity.ok(resp);
    }

    @PostMapping("/namespace-delete")
    public ResponseEntity<ResponseDTO> delete(@RequestBody NameSpaceDTO dto) {
        if (StringUtils.isEmpty(dto.getUid())) {
            return ResponseDTO.failure("命名空间ID不能为空");
        }
        ResponseDTO resp = nameSpaceService.customDelete(dto);
        return ResponseEntity.ok(resp);
    }

}
